<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class AudioController extends BaseController {

    //音频列表
    public function index(){
        $data = array_filter($_GET);
        if (!empty($_GET['audio_name'])) {
            $data['audio_name'] = array('like',"%".trim($_GET['audio_name'])."%");
        }
        unset($data['p']);
        $data['audio_display'] = 1;
        $count = M('audio')->where($data)->count();
        $page = getpage($count,10);
        $audio_arr = M('audio')->where($data)->order('audio_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('audio_arr', $audio_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加音频
    public function audio_add(){
        if (I('post.action') == 'audio_add_action') {
            $this->audio_add_action();
            exit();
        }
        $this->display();
    }

    //添加音频处理
    public function audio_add_action(){
        if($_POST != array_filter($_POST)){
            $this->error('音频选项不能有空值！');
        }
        //图片文件上传
        // $finfoicon = upload($_FILES['audio_icon']);
        // $finfopic = upload($_FILES['audio_pic']);
        // //如果有一个上传失败，则把成功的文件也删除掉
        // if ($finfoicon['status'] == 0 || $finfopic['status'] == 0) {
        //     unlink($finfoicon['filepath']);
        //     unlink($finfopic['filepath']);
        // }
        // //音频图标上传
        // if ($finfoicon['status'] == 0) {
        //     if ($finfoicon['msg'] == 'nofile') {
        //         $this->error('音频图标未上传');
        //     }
        //     $this->error($finfoicon['msg']);
        // } elseif ($finfoicon['status'] == 1) {
        //     $_POST['audio_icon'] = $finfoicon['filepath'];
        // }
        // //音频封面图上传
        // if ($finfopic['status'] == 0) {
        //     if ($finfopic['msg'] == 'nofile') {
        //         $this->error('音频封面图未上传');
        //     }
        //     $this->error($finfopic['msg']);
        // } elseif ($finfopic['status'] == 1) {
        //     $_POST['audio_pic'] = $finfopic['filepath'];
        // }
        //音频文件上传
        $finfoaudio1 = upload($_FILES['audio_file1'],3);
        if ($finfoaudio1['status'] == 0 && $finfoaudio1['msg'] != 'nofile') {
            $this->error($finfoaudio1['msg']);
        }elseif ($finfoaudio1['status'] == 1) {
            $_POST['audio_file1'] = $finfoaudio1['filepath'];
        }
        $finfoaudio2 = upload($_FILES['audio_file2'],3);
        if ($finfoaudio2['status'] == 0 && $finfoaudio2['msg'] != 'nofile') {
            $this->error($finfoaudio2['msg']);
        }elseif ($finfoaudio2['status'] == 1) {
            $_POST['audio_file2'] = $finfoaudio2['filepath'];
        }
        $finfoaudio3 = upload($_FILES['audio_file3'],3);
        if ($finfoaudio3['status'] == 0 && $finfoaudio3['msg'] != 'nofile') {
            $this->error($finfoaudio3['msg']);
        }elseif ($finfoaudio3['status'] == 1) {
            $_POST['audio_file3'] = $finfoaudio3['filepath'];
        }
        $_POST['audio_time'] = date('Y-m-d H:i:s',time());

        if(M('audio')->add($_POST)){
            $this->success('添加音频成功','index');
        }else{
            $this->error('添加音频失败');
        }

    }

    //音频详情
    public function audio_details(){
        $data['audio_id'] = I('get.audio_id');
        $audio_arr = M('audio')->where($data)->find();
        $this->assign('audio_arr',$audio_arr);
        $this->display();
    }

    //删除音频
    public function audio_del(){
        $data['audio_id'] = I('get.audio_id');
        $data['audio_display'] = 0;
        M('audio')->save($data);
        $this->redirect('index');
    }

    //修改音频
    public function audio_edit(){
        if (I('post.action') == 'audio_edit_action') {
            $this->audio_edit_action();
            exit();
        }
        $data['audio_id'] = I('get.audio_id');
        $audio_arr = M('audio')->where($data)->find();
        $this->assign('audio_arr',$audio_arr);
        $this->display();
    }

    //修改音频操作
    public function audio_edit_action(){
        if($_POST != array_filter($_POST)){
            $this->error('音频选项不能有空值！');
        }
        // //文件上传
        // $finfoicon = upload($_FILES['audio_icon']);
        // $finfopic = upload($_FILES['audio_pic']);
        // //音频图标上传
        // if ($finfoicon['status'] == 0 && $finfoicon['msg'] != 'nofile') {
        //     $this->error($finfoicon['msg']);
        // } elseif ($finfoicon['status'] == 1) {
        //     $_POST['audio_icon'] = $finfoicon['filepath'];
        // }
        // //音频封面图上传
        // if ($finfopic['status'] == 0 && $finfopic['msg'] != 'nofile') {
        //     $this->error($finfopic['msg']);
        // } elseif ($finfopic['status'] == 1) {
        //     $_POST['audio_pic'] = $finfopic['filepath'];
        // }
        //音频文件上传
        $finfoaudio1 = upload($_FILES['audio_file1'],3);
        if ($finfoaudio1['status'] == 0 && $finfoaudio1['msg'] != 'nofile') {
            $this->error($finfoaudio1['msg']);
        }elseif ($finfoaudio1['status'] == 1) {
            $_POST['audio_file1'] = $finfoaudio1['filepath'];
        }
        $finfoaudio2 = upload($_FILES['audio_file2'],3);
        if ($finfoaudio2['status'] == 0 && $finfoaudio2['msg'] != 'nofile') {
            $this->error($finfoaudio2['msg']);
        }elseif ($finfoaudio2['status'] == 1) {
            $_POST['audio_file2'] = $finfoaudio2['filepath'];
        }
        $finfoaudio3 = upload($_FILES['audio_file3'],3);
        if ($finfoaudio3['status'] == 0 && $finfoaudio3['msg'] != 'nofile') {
            $this->error($finfoaudio3['msg']);
        }elseif ($finfoaudio3['status'] == 1) {
            $_POST['audio_file3'] = $finfoaudio3['filepath'];
        }

        if(M('audio')->save($_POST)){
            $this->success('修改音频成功','index');
        }else{
            $this->error('没有信息被修改');
        }
    }



}